package com.house.web.converter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.core.convert.converter.Converter;

public class DateConverter implements Converter<String, Date> {
	private String[] datePattern;
	
	//由配置文件注入
	public void setDatePattern(String[] datePattern) {
		this.datePattern = datePattern;
	}

	public Date convert(String value) {
		if(value!=null && value!=""){
			for (String string : datePattern) {
			SimpleDateFormat sdf=new SimpleDateFormat(string);
			try {
				Date date=sdf.parse(value);
				return date;
			} catch (ParseException e) {
				continue;
			}
		}
		throw new RuntimeException("日期格式不正确");
		}
		return null;
	}
	
}
